package pkg;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.util.Date;
import java.util.Iterator;
import java.util.Stack;
import java.util.Vector;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.JspFragment;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import Utility.Utility;
import java.util.List;
import java.util.Set;
import xmlMapping.*;
/**
 *
 * @author Mostafa
 */
public class DisplayCategoriesTagHandler extends SimpleTagSupport {

    /**
     * Called by the container to invoke this tag. 
     * The implementation of this method is provided by the tag library developer,
     * and handles all tag processing, body iteration, etc.
     */
    @Override
    public void doTag() throws JspException {
        JspWriter out = getJspContext().getOut();
        Utility u=new Utility();
        List categories =u.getCategories();
        String result="";
       
        try {
            // TODO: insert code to write html before writing the body content.
           
         
            
            
         Iterator<Category> itr=(Iterator<Category>) categories.iterator();
         
             
             while (itr.hasNext()) {
                 
                Category curr = itr.next();
                
         
                System.out.println("CATTTTT  =   "+curr.getName());
                
            result+="<div class=\"forabg\"><div class=\"inner\"><span class=\"corners-top\"><span></span></span> <ul class=\"topiclist\"> "
            + "<li class=\"header\"><dl class=\"icon\"><dt>"
            + curr.getName()+"&nbsp;&nbsp;<a href=\"#\" class=\"edit-icon\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</a>&nbsp;&nbsp;<a href=\"#\" class=\"delete-icon\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</a></dt>"
           +"<dd class=\"topics\">Topics</dd> "
           + "<dd class=\"posts\">Posts</dd> "
         + "<dd class=\"lastpost\"><span>Last post</span></dd> 	</dl></li></ul> <ul class=\"topiclist forums\"> ";
           
           
	
                        
                        /////////////////////////   forum //////// 
           
          Iterator<Forum> fs=curr.getForums().iterator();
          System.out.println(curr.getForums().size());
          int i=0;
                 while (fs.hasNext()) { 
                     Forum f = fs.next();
      //               f=new Forum(new Category("dasd", "dsada"), "dasda  ", "dasdas");
                     String lastname="";
                     String lastdate="";
                     int posts=0;
                     int threads=0;
                   if(f.getPost()!=null)
                   { lastname=f.getPost().getMember().getUserName();
                   lastdate=f.getPost().getLastModified().toString();
                   }
                   else{lastname="Not Post Added yet.";}
                     if (f.getNoofpost()!=null) {
                         
                         posts=f.getNoofpost();
                         threads=f.getNoofthread();
                     }
                   
                   
                     
                 result+="  <li class=\"row\"><dd> &nbsp;&nbsp;<input type=\"checkbox\"/> </dd>"
  + "<dl class=\"icon\" style=\"background-image: url(./styles/carbon_fiber/imageset/forum_read.png); background-repeat: no-repeat;\"> "
  + "<dt title=\"No new posts\"> "
 + "<a href=\"ViewForum.jsp?forumid="+f.getForumId()+"\" class=\"forumtitle\">"+f.getName()+"</a><br/>"
+ f.getDescription() 
 + "</dt> <dd class=\"topics\">"+threads+"<dfn>Topics</dfn></dd> <dd class=\"posts\">"+posts+" <dfn>Posts</dfn></dd>"
 + "<dd class=\"lastpost\"><span> <dfn>Last post</dfn> by <a href=\"memberlist.html\" style=\"color: #AA0000;\" class=\"username-coloured\">"+lastname+"</a>"
 + "<a href=\"ViewTopic.jsp\"><img src=\"imageset/icon_topic_latest.gif\" width=\"11\" height=\"9\" alt=\"View the latest post\" title=\"View the latest post\" />"
  + "</a> <br />"+lastdate+"</span> </dd> </dl> </li>";

        
                   System.out.println(i);
                   
                 }
		
	
		////////////////////////////////////////////////////////////////////// 
	
	result+="</ul> </div> </div> ";
            }
             
            out.print(result);

        } catch (Exception ex) {
            throw new JspException("Error in DisplayCategoriesTagHandler tag", ex);
        }
    }
}
